<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名" prop="clientCompany">
        <el-input
          v-model="queryParams.clientCompany"
          placeholder="请输入公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户行业" prop="clientIndustry">
        <el-input
          v-model="queryParams.clientIndustry"
          placeholder="请输入客户行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户需求产品" prop="clientProduct">
        <el-input
          v-model="queryParams.clientProduct"
          placeholder="请输入客户需求产品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="客户分类" prop="clientClassify">
        <el-select v-model="queryParams.clientClassify" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            clearable
            @keyup.enter.native="handleQuery">

          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:client_pool:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:client_pool:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!-- 卡片-->
    <el-card class="box-card" v-for="key in client_poolList">
      <div slot="header" class="clearfix">
        <span @click="particulars(key)">{{key.clientCompany}}</span>
        <el-button type="primary" style="float: right;" icon="el-icon-delete" circle @click="removeclientmod(key.clientId)"></el-button>
      </div>

      <div v-for="keyclinet in key.crmClientPhoneList">
        <div  class="text item">
          <span>电话:</span><a :href="'tel:'+keyclinet.clientPhone" style="color: #00afff">{{keyclinet.clientPhone}}</a>
        </div>
        <div @click="particulars(key)">
        <div  class="text item">
          <span>客户名:</span>{{keyclinet.clientName}}
        </div>
      </div>
      <div  class="text item">
        <span>客户行业:</span>{{key.clientIndustry}}
      </div>
      <div  class="text item">
        <span>客户需求:</span>{{key.clientProduct}}
      </div>
      <div  class="text item">
        <span>客户分类:</span>{{key.clientClassify}}
      </div>
      <div  class="text item">
        <span>客户地址:</span>{{key.clientAddress}}
      </div>

      <div  class="text item" style="float: right">
        <span>创建时间:</span>{{key.createTime}}
      </div>
      </div>
    </el-card>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户公海池对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名" prop="clientCompany">
          <el-input v-model="form.clientCompany" placeholder="请输入客户公司名" />
        </el-form-item>








        <el-form-item label="客户行业" prop="clientIndustry">
          <el-input v-model="form.clientIndustry" placeholder="请输入客户行业" />
        </el-form-item>
        <el-form-item label="客户需求" prop="clientProduct">
          <el-input v-model="form.clientProduct" placeholder="请输入客户需求产品" />
        </el-form-item>
        <el-form-item label="客户地址" prop="clientAddress">
          <el-input v-model="form.clientAddress" placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="客户分类" prop="clientClassify">
          <el-select v-model="form.clientClassify" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable
              @keyup.enter.native="handleQuery">

            </el-option>
          </el-select>
        </el-form-item>










        <el-divider content-position="center">客户信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCrmClientPhone">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCrmClientPhone">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="crmClientPhoneList" :row-class-name="rowCrmClientPhoneIndex" @selection-change="handleCrmClientPhoneSelectionChange" ref="crmClientPhone">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="客户名" prop="clientName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.clientName" placeholder="请输入客户名" />
            </template>
          </el-table-column>
          <el-table-column label="客户电话" prop="clientPhone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.clientPhone" placeholder="请输入客户电话" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClient_pool, getClient_pool, delClient_pool, addClient_pool, updateClient_pool } from "@/api/crm/client_pool";
import {addClient, mylistClient_pool, removeclient} from "../../../api/crm/client_pool";

export default {
  name: "myclient",
  data() {
    return {
      activeName: 'first',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      options: [{
        value: 'A',
        label: 'A类客户'
      }, {
        value: 'B',
        label: 'B类客户'
      }, {
        value: 'C',
        label: 'C类客户'
      }],
      // 子表选中数据
      checkedCrmClientPhone: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户公海池表格数据
      client_poolList: [],
      // ${subTable.functionName}表格数据
      crmClientPhoneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientCompany: null,
        clientPhoneId: null,
        clientIndustry: null,
        clientProduct: null,
        clientConditionId: null,
        clientAddress: null,
        clientClassify: null,
        keyboarderId: null,
        keyboarder: null,
        clientVestInId: null,
        clientVestIn: null,
        clientStatus: null,
        creationBy: null,
        upadteBy: null,
        crmClientPhoneList:[]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户公海池列表 */
    getList() {
      this.loading = true;
      mylistClient_pool(this.queryParams).then(response => {
        this.client_poolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //点击卡片查看详情
    particulars(row){
//crm/client_pool/index
      this.reset();
      const clientId=row.clientId
        this.$router.push(`client_particulars/index/`+clientId)
},
    removeclientmod(row){
      this.form.clientId=row
      removeclient(this.form).then(response => {
        this.$modal.msgSuccess("移交到公海池成功");
        this.open = false;
        this.getList();
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        clientCompany: null,
        clientPhoneId: null,
        clientIndustry: null,
        clientProduct: null,
        clientConditionId: null,
        clientAddress: null,
        clientClassify: null,
        keyboarderId: null,
        keyboarder: null,
        clientVestInId: null,
        clientVestIn: null,
        clientStatus: null,
        createTime: null,
        creationBy: null,
        updateTime: null,
        upadteBy: null,
        remark: null
      };
      this.crmClientPhoneList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户公海池";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids
      getClient_pool(clientId).then(response => {
        this.form = response.data;
        this.crmClientPhoneList = response.data.crmClientPhoneList;
        this.open = true;
        this.title = "修改客户公海池";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.crmClientPhoneList = this.crmClientPhoneList;
          if (this.form.clientId != null) {
            updateClient_pool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClient_pool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientIds = row.clientId || this.ids;
      this.$modal.confirm('是否确认删除客户公海池编号为"' + clientIds + '"的数据项？').then(function() {
        return delClient_pool(clientIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowCrmClientPhoneIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddCrmClientPhone() {
      let obj = {};
      obj.clientName = "";
      obj.clientPhone = "";
      obj.remark = "";
      this.crmClientPhoneList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteCrmClientPhone() {
      if (this.checkedCrmClientPhone.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const crmClientPhoneList = this.crmClientPhoneList;
        const checkedCrmClientPhone = this.checkedCrmClientPhone;
        this.crmClientPhoneList = crmClientPhoneList.filter(function(item) {
          return checkedCrmClientPhone.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCrmClientPhoneSelectionChange(selection) {
      this.checkedCrmClientPhone = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/client_pool/export', {
        ...this.queryParams
      }, `client_pool_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
